package com.hsy.baoinbao.modules.main.ui.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;

import com.deparse.dpframe.ui.fragment.DPBaseFragment;
import com.deparse.dpframe.ui.view.DPTabItemContainer;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.application.BIBApplication;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.modules.login.LoginActivity;
import com.hsy.baoinbao.modules.main.ui.fragment.FragmentHome;
import com.hsy.baoinbao.modules.my.fragment.FragmentMy;
import com.hsy.baoinbao.modules.order.fragment.FragmentGoods;
import com.hsy.baoinbao.modules.order.fragment.FragmentReceiving;

public class MainActivity extends CommonBaseActivityWithSimpleActionBar {

    public DPTabItemContainer tab;
    private Class<?>[] fragments = {
            FragmentHome.class,
            FragmentReceiving.class,
            FragmentGoods.class,
            FragmentMy.class
    };
    private int[] titles = {
            R.string.txt_home,
            R.string.txt_receiving,
            R.string.txt_goods,
            R.string.txt_my
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setLeftButtonVisiblity(View.GONE);
        setTitle(R.string.txt_home);
        setRightButtonVisiblity(View.GONE);
        setRightButtonText("添加");
        setRightButtonTextColor(R.color.white);

        tab = F(R.id.tabs);
        tab.setOnTabChangeListener(new DPTabItemContainer.OnTabChangeListener() {
            @Override
            public void beforeTabChange(int index, View tabWidget) {

            }

            @Override
            public void afterTabChange(int index, View tabWidget) {
                try {
                    if (0 == index || 3 == index) {
                        setRightButtonVisiblity(View.GONE);
                    } else {
                        setRightButtonVisiblity(View.VISIBLE);
                    }
                    setTitle(titles[index]);
                    DPBaseFragment fragment = (DPBaseFragment) fragments[index].newInstance();
                    fragment.setAttchedActivity(MainActivity.this);
                    replaceFragment(R.id.container, fragment);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        tab.setCurrentTab(0);

        String[] extra = getActivityStartExtra();
        if (!isEmpty(extra)) {
            if (Consts.JUMP_TO_CYCLE_DONE.equals(extra[0])) {

                try {
                    setTitle(titles[1]);
                    DPBaseFragment fragment = null;
                    fragment = (DPBaseFragment) fragments[1].newInstance();
                    fragment.setAttchedActivity(MainActivity.this);

                    tab.setCurrentTab(1);

                    Bundle bundle = new Bundle();
                    bundle.putInt(Consts.TO_SHOW_FRAGMENT, 1);
                    fragment.setArguments(bundle);
                    replaceFragment(R.id.container, fragment);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            } else if (Consts.JUMP_TO_CYCLE_CANCEL.equals(extra[0])) {

                try {
                    setTitle(titles[1]);
                    DPBaseFragment fragment = null;
                    fragment = (DPBaseFragment) fragments[1].newInstance();
                    fragment.setAttchedActivity(MainActivity.this);

                    tab.setCurrentTab(1);

                    Bundle bundle = new Bundle();
                    bundle.putInt(Consts.TO_SHOW_FRAGMENT, 2);
                    fragment.setArguments(bundle);
                    replaceFragment(R.id.container, fragment);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        DPLogUtil.d("onActivityResult requestCode=" + requestCode + ",resultCode=" + resultCode);
        super.onActivityResult(requestCode, resultCode, data);
        if (1 == requestCode && (1 == resultCode || 2 == resultCode)) {
            tab.setCurrentTab(1);
            try {
                setTitle(titles[1]);
                DPBaseFragment fragment = (DPBaseFragment) fragments[1].newInstance();
                fragment.setAttchedActivity(MainActivity.this);
                fragment.onActivityResult(requestCode, resultCode, data);

                Bundle bundle = new Bundle();
                bundle.putInt(Consts.TO_SHOW_FRAGMENT, resultCode);
                fragment.setArguments(bundle);
                replaceFragment(R.id.container, fragment);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (2 == requestCode && 2 == resultCode) {
            tab.setCurrentTab(2);
            try {
                setTitle(titles[2]);
                DPBaseFragment fragment = (DPBaseFragment) fragments[2].newInstance();
                fragment.setAttchedActivity(MainActivity.this);
                fragment.onActivityResult(requestCode, resultCode, data);

                Bundle bundle = new Bundle();
                bundle.putInt(Consts.TO_SHOW_FRAGMENT, resultCode);
                fragment.setArguments(bundle);
                replaceFragment(R.id.container, fragment);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showConfirmDialog();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    private void showConfirmDialog() {
        View dialogView = getLayoutInflater().inflate(R.layout.dialog_confirm, null);
        final Dialog dialog = new AlertDialog.Builder(MainActivity.this)
                .setView(dialogView).create();

        TextView title = (TextView) dialogView.findViewById(R.id.confirm_dialog_title);
        TextView cancel = (TextView) dialogView.findViewById(R.id.confirm_dialog_cancel);
        TextView confirm = (TextView) dialogView.findViewById(R.id.confirm_dialog_ok);

        title.setText("确定退出吗?");
        confirm.setTextColor(getResources().getColor(R.color.tab_text_color));
        cancel.setTextColor(getResources().getColor(R.color.tab_text_color));

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        dialog.show();
    }

    @Override
    public boolean onActionBarRightButtonClicked(ImageButton rightButton) {

        int currentTabIndex = tab.getCurrentTabIndex();

        if (1 == currentTabIndex) {
            Intent intent = new Intent(this, IntoStepFirstActivity.class);
            startActivity(intent);
        } else if (2 == currentTabIndex) {
            Intent intent = new Intent(this, GoodsIntoFirstActivity.class);
            startActivity(intent);
        }

        return super.onActionBarRightButtonClicked(rightButton);


    }
}
